; Calculate Linear Address
; Called with following Values:
;       ax - has segment number
;	bx - has offset number
; 
; Output:
;	ecx - has Linear Address
; 
; We are in 16 bit mode
;
; Linear Address = Shift Segment by 1 byte and add Offset to it
; So 0x07c0:0x0  = 0x7c00

LinearAdd:
	xor ecx,ecx
	mov cx,ax
	shl ecx,4
	and ebx,0x0000FFFF
	add ecx,ebx
	ret


; Filling Global Descriptor Table
; -------------------------------
;
; Note:
; -----
; 1. Order of variables input: Base (32 bits), Limit (20 bits), Access Rights(8 bits), Flags(4 bits), Segment Address 
;                                                                                                     (32 bits)
; 
; 2. Variables input to function are moved LS 4 bits First (Right to Left) to table
;
; 3. While reading LS 4 bits or MS 4 bits, read from left to right
;
; Base:
; -----
; Bits in Table          Bits in Field 'Base'        Location w.r.t Beginning of Segment
;---------------------------------------------------------------------------------------
; 16 - 31                0  - 15                     +2 (2 bytes in length)
; 32 - 39                16 - 23                     +4 (1 byte in length)
; 55 - 63                24 - 31                     +7 (1 byte in length)
;
;
; Howto:
; ------
;
; Below %5 ie., the fifth variable input to function GDTFill is Beginning of GDT's Code Segment
; [%5+2] represents entry 1 in above table
; [%5+4] represents entry 2 in above table
; [$5+7] represents entry 3 in above table
; Entry 1 is 2 bytes (so ax is moved to word [%5+2]
; Entry 2 and 3 are 1 byte (so al is moved to byte[%5+4] and [%5+7] respectively
;
; Limit:
; ------
;
; Bits in Table          Bits in Field 'Limit'        Location w.r.t Beginning of Segment
;---------------------------------------------------------------------------------------
; 0  -  15               0 -  15                       +0 (2 bytes in length)
; 16 -  20               48 - 51                       +6 (1 byte in length)
; 
; Refer 'Howto' above for detailed description
;
; Access Rights:
; --------------
; 
; Access Rights (Bit 40 to Bit 47) = Type (Bit 40 to Bit 43) + System Flag (Bit 44) + DPL (Bit 45 and 46) + Reserved (Bit 47); 
; Type        - Your Call (Say A is Kernel Code and B is User code (Here it is 11 and 3 respectively)
; System Flag - Both Code and Data Segment have S Flag = 1
; DPL         - Privilege level (Ring 0 or 3?) (Ring 0 - 00 and Ring 3 - 11)
; 
; Flags:
; ------
; G B O AVL
;
; G   - Granularity = 1 here (means Segment Size is 4096 bytes)
; B   - Address offsets used for accessing segments are 32 bits long
; O   - 0 (Don't know what it is!)
; AVL - 1 here (You Can Ignore it)
;
; 

%macro GDTFill	5	
	push eax
; Base
	mov eax,%1
	mov word [%5+2],ax
	shr eax,16         ; Shift Right to 
	mov byte [%5+4],al
	shr eax,8
	mov byte [%5+7],al
; Limit
	mov eax,%2
	and eax,0x000FFFFF
	mov word [%5],ax	; ecrit (0..15)
	shr eax,16			; place (16..19) sur le nibble inferieur
	mov byte [%5+6],0	; initialise flags+lim(16..19) a 0
	or [%5+6],al		; ecrit (16..19)
; flags :
	mov al,%4
	and al,0x0F
	shl al,4
	or [%5+6],al
; acces :
	mov byte [%5+5],%3
	pop eax
%endmacro

